﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xant.FlowEngine.Interface;

namespace Xant.OrganzService
{
    public sealed class Organization : IOrganization
    {
        #region 事件声明
        public delegate void StaffMembersInquiringHandler(Organization organz);
        internal StaffMembersInquiringHandler OnStaffMembersInquiring;
        public delegate void SubordinationsInquiringHandler(Organization organz);
        internal SubordinationsInquiringHandler OnSubordinationsInquiring;
        #endregion

        private List<IOrganization> subordinations;
        internal List<IStaffMember> staffMembers;

        internal Guid id;
        private IOrganization superior;

        public object Id
        {
            get { return id; }
            set { id = (Guid)value; }
        }

        public string No { get; set; }

        internal string ParentNo { get; set; } 

        public string Code { get; set; }

        public string Name { get; set; }

        /// <summary>
        /// 向最终用户显示的友好名称
        /// <remarks>如果是公司则直接显示{公司名称}，如果是部门则显示{所属公司名称.部门名称}，如果是虚拟组织则显示{上级公司名称.虚拟组织名称}</remarks>
        /// </summary>
        public string FriendlyName
        {
            get
            {
                switch (this.Type)
                {
                    case OrganizType.Corporation:
                        return this.Name;
                    case OrganizType.Department:
                    case OrganizType.Fictitious:
                        return string.Format("{0}.{1}", this.Corporation.Name, this.Name);
                    default:
                        throw new ApplicationException("未处理的组织机构类型："+this.Type.ToString());
                }
            }
        }

        public string CodeEx { get; set; }

        public OrganizType Type { get; set; }

        public string CustomTag { get; set; }

        internal string ExternalId { get; set; }


        public Organization(){
             subordinations = new List<IOrganization>();
             staffMembers = new List<IStaffMember>();
        }

        /// <summary>
        /// 添加直接下级机构/部门
        /// </summary>
        /// <param name="organz"></param>
        public void AddSubordination(params IOrganization[] items)
        {
            foreach (var item in items)
            {
                if (this.subordinations.Contains(item))
                    return;
                this.subordinations.Add(item);
                (item as Organization).Superior = this;
            }
        }

        /// <summary>
        /// 直接下级机构
        /// </summary>
        public IEnumerable<IOrganization> Subordinations
        {
            get {
                if (OnSubordinationsInquiring != null)
                {
                    OnSubordinationsInquiring(this);
                }
                return subordinations;
            }
        }

        /// <summary>
        /// 直接上级机构
        /// </summary>
        public IOrganization Superior
        {
            get { return superior; }
            set
            {
                if (superior == value)
                    return;
                this.superior = value;
                (this.superior as Organization).AddSubordination(this);
            }
        }

        /// <summary>
        /// 所属公司
        /// <remarks>如果该组织机构类型为公司，则返回自己，否则返回最靠近的上级公司</remarks>
        /// </summary>
        public IOrganization Corporation
        {
            get
            {
                if (this.Type == OrganizType.Corporation)
                    return this;
                return Superior == null ? null : Superior.Corporation;
            }
        }

        public IEnumerable<IStaffMember> StaffMembers
        {
            get {
                if(OnStaffMembersInquiring!=null)
                {
                    OnStaffMembersInquiring(this);
                }
                return staffMembers;
            }
        }

    }
}
